package com.gitee.wsl.common.chart.render.shape.line

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PointF
import com.gitee.wsl.android.ui.ext.type.drawLine
import com.gitee.wsl.common.chart.ChartEnum
import com.gitee.wsl.common.ui.draw.type.LineSpacingStyle
import kotlin.math.abs
import com.gitee.wsl.mathematics.geometry.shape.Rectangle

/**
 * @ClassName DyLine
 * @Description 交叉线基类
 * @author XiongChuanLiang<br/>(xcl_168@aliyun.com)
 *
 */
class CrossLine: Rectangle() {

     val linePaint: Paint by lazy {
          Paint(Paint.ANTI_ALIAS_FLAG).apply {
              color = Color.rgb(215, 10, 10) //50, 165, 238));			
          }
     }
    
     var centerXY: PointF= PointF()

    //设置交叉线显示风格
     var dyLineStyle = ChartEnum.DyLineStyle.Cross

    //线绘制为实线，虚实线 	等哪种风格
     var lineDrawStyle = LineSpacingStyle.Solid

     var oldX = 0.0f
     var oldY = 0.0f

    /**
     * 点击位置坐标
     * @param x        x坐标
     * @param y        y坐标
     */
    fun setCurrentXY(x: Float, y: Float) //setCurrentXY
    {
        centerXY.x = x
        centerXY.y = y
    }

    /**
     * 是否需要重绘
     * @return 是否允许重绘
     */
    val isInvalidate: Boolean
        get() {
            if (abs(centerXY.x - oldX).compareTo(5f) == 1 || abs(centerXY.y - oldY).compareTo(5f) == 1) {
                oldX = centerXY.x
                oldY = centerXY.y
                return true
            }
            return false
        }


    // Cross 指定交叉的水平线和垂直线。
    // BackwardDiagonal 从右上到左下的对角线的线条图案。
    // Vertical		垂直线
    // Horizontal 水平线
    private fun drawCross(canvas: Canvas) {
        //竖线
        drawVertical(canvas)
        //横线
        drawHorizontal(canvas)
    }

    private fun drawBackwardDiagonal(canvas: Canvas) {

        //竖线
        canvas.drawLine(
            lineDrawStyle,
            centerXY.x, centerXY.y, centerXY.x, bottom,  linePaint
        )
        //横线
        canvas.drawLine(
            lineDrawStyle,
            left, centerXY.y, centerXY.x, centerXY.y,  linePaint
        )
    }

    private fun drawVertical(canvas: Canvas) //竖线
    {
        canvas.drawLine(
            lineDrawStyle,
            centerXY.x, top, centerXY.x, bottom,  linePaint
        )
    }

    private fun drawHorizontal(canvas: Canvas) {
        canvas.drawLine(
            lineDrawStyle,
            left, centerXY.y, right, centerXY.y, linePaint
        )
    }

    fun renderLine(canvas: Canvas, left: Float, top: Float, right: Float, bottom: Float) {
        
        //if(Float.compare(left, 0.0f) == 0 && Float.compare(top, 0.0f) == 0 &&
        //	Float.compare(right, 0.0f) == 0 && Float.compare(bottom, 0.0f) == 0) return;
        if (centerXY.x.compareTo(left) == 0 ||
            centerXY.x.compareTo(left) == -1 ||
            centerXY.x.compareTo(right) == 0 ||
            centerXY.x.compareTo(right) == 1 ||
            centerXY.y.compareTo(top) == 0 ||
            centerXY.y.compareTo(top) == -1 ||
            centerXY.y.compareTo(bottom) == 0 ||
            centerXY.y.compareTo(bottom) == 1
        ) return
        this.left = left
        this.top = top
        this.right = right
        this.bottom = bottom
        when (dyLineStyle) {
            ChartEnum.DyLineStyle.Cross -> drawCross(canvas)
            ChartEnum.DyLineStyle.BackwardDiagonal -> drawBackwardDiagonal(canvas)
            ChartEnum.DyLineStyle.Vertical -> drawVertical(canvas)
            ChartEnum.DyLineStyle.Horizontal -> drawHorizontal(canvas)
        }
    }
    
}